﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });

    $("#Title").val($("#TitleDefault").val());
    $("#State").val($("#StateDefault").val());
    $("#CardType").val($("#CardTypeDefault").val());
    $("#CardExpireMonth").val($("#CardExpireMonthDefault").val());
    $("#CardExpireYear").val($("#CardExpireYearDefault").val());
});

function Submit() {

    $("#submit").hide();

    var input = {
        Title: $("#Title").val(),
        FirstName: $("#FirstName").val(),
        LastName: $("#LastName").val(),
        Street: $("#Street").val(),
        Street2: $("#Street2").val(),
        City: $("#City").val(),
        State: $("#State").val(),
        ZipCode: $("#ZipCode").val(),
        Phone: $("#Phone").val(),
        CardHolderName: $("#CardHolderName").val(),
        CardType: $("#CardType").val(),
        CardNumber: $("#CardNumber").val(),
        CardExpireMonth: $("#CardExpireMonth").val(),
        CardExpireYear: $("#CardExpireYear").val(),
        CardSecurityCode: $("#CardSecurityCode").val()
    };

    Ajax({
        url: "/MyAdams/Profile/SaveCreditCard",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                var ReturnTo = $("#ReturnTo").val();
                if (ReturnTo.length == 0) {
                    location.href = "/MyAdams/Profile?msg=1";
                } else {
                    location.href = "/MyAdams/Donations/Donate";
                }
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}